package com.arron.ssm.controller;

import com.arron.ssm.entity.Employee;
import com.arron.ssm.service.EmployeeService;
import com.arron.ssm.valided.AddGroup;
import com.arron.ssm.valided.UpdateGroup;
import com.arron.ssm.vo.ResultObjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname EmployeeController
 * @Date 2020/9/23 18:17
 * @Created by 刘
 * 员工信息的增删改查
 */
@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;


    //查询所有员工
    @ResponseBody
    @GetMapping("/emps")
    public List<Employee> emps(){
        return employeeService.queryAllEmp();
    }
    //按照id查询员工
    @ResponseBody
    @GetMapping("/emp/{id}")
    public Employee empbyId(@PathVariable(value = "id") Long id){
          return  employeeService.queryEmpById(id);
    }

    //新增员工,并使用jsr303校验数据是否合法
    @ResponseBody
    @PostMapping("/emp")
    public ResultObjVo addEmp(@Validated(value = {AddGroup.class}) @RequestBody Employee employee, BindingResult result){

        if (result.hasErrors()) {
            ResultObjVo repMsg = new ResultObjVo();
            Map<String,String> map = new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            repMsg.setCode(-1);
            repMsg.setMaps(map);
            return repMsg;
        }else {
            //如果数据合法的话再进行添加
            employeeService.addEmp(employee);
            return new ResultObjVo(0,"添加员工成功");
        }
    }

    //删除
    @DeleteMapping("/emp/{id}")
    @ResponseBody
    public ResultObjVo delete(@PathVariable("id") Long id){

        if (employeeService.removeById(id)>0){
            return new ResultObjVo(0,"删除成功");
        }else {
            return new ResultObjVo(-1,"删除失败");
        }

    }


    //修改员工信息
    @PostMapping("/update/emp/{id}")
    @ResponseBody
    public ResultObjVo putEmp(@Validated(value = UpdateGroup.class) @RequestBody Employee employee,@PathVariable("id") Long id,BindingResult result){
        if (result.hasErrors()) {
            ResultObjVo repMsg = new ResultObjVo();
            Map<String, String> map = new HashMap<>();
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            repMsg.setCode(-1);
            repMsg.setMaps(map);
            return repMsg;
        }
        if (employeeService.updateById(employee,id)>0){
            return new ResultObjVo(0,"修改成功");
        }else {
            return new ResultObjVo(-1,"修改失败");
        }
    }



}
